package org.rbac.action;

import org.rbac.serv.AuthorityServices;
import org.rbac.vo.ActionReturn;
import org.rbac.vo.Authority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 权限管理
 * <pre>
 * 项目名称：rbac
 * 类名称：AuthorityAction
 * 类描述：权限管理
 * 创建人：唐家林(tjl)
 * 创建时间：2014年10月8日　下午3:37:14
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Controller
@RequestMapping(value = "/authority")
public class AuthorityAction {

	@Autowired
	private AuthorityServices authorityServ;

	/**
	 * 修改
	 * 
	 * @param a
	 *            修改的对象数据
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public String modifyAuthority(Authority a) {
		ActionReturn ar = new ActionReturn();
		try {
			authorityServ.moidifyAuthority(a);

			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 启用
	 * 
	 * @param ids
	 *            启用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/enable")
	@ResponseBody
	public String enableAuthority(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 1;
			authorityServ.moidifyAuthorityStatus(ids, status);

			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 禁用
	 * 
	 * @param ids
	 *            禁用的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/disenable")
	@ResponseBody
	public String disenableAuthority(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 2;
			authorityServ.moidifyAuthorityStatus(ids, status);

			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 *            删除的对象数据主键
	 * @return 返回操作结果
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String deleteAuthority(Integer[] ids) {
		ActionReturn ar = new ActionReturn();
		try {
			short status = 0;
			authorityServ.moidifyAuthorityStatus(ids, status);
			ar.setStatus(true);
			ar.setMsg("操作成功");
		} catch (Exception e) {
			ar.setStatus(false);
			ar.setMsg("操作失败");
			e.printStackTrace();
		}
		return ar.toString();
	}

	/**
	 * 进入列表
	 * 
	 * @return 返回视图
	 */
	@RequestMapping(value = "/list")
	public ModelAndView actionList() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/rbac/authority");
		return mav;
	}
	/**
	 * 权限绑定资源
	 * @param aid
	 * @param resid
	 * @return
	 */
	@RequestMapping(value = "/resBindAuth")
    @ResponseBody
    public String resBindAuth(Integer aid, @RequestParam("resid[]")Integer[] resid) {
	    ActionReturn ar = new ActionReturn();
        try {
            authorityServ.resBindAuth(aid, resid);
            ar.setStatus(true);
            ar.setMsg("操作成功");
        } catch (Exception e) {
            ar.setStatus(false);
            ar.setMsg("操作失败");
            e.printStackTrace();
        }
        return ar.toString();
	}
	/**
	 * 移除权限的资源
	 * @param resid
	 * @return
	 */
	@RequestMapping(value = "/deleteAuthOfRes")
    @ResponseBody
    public String deleteAuthOfRes( @RequestParam("resid[]")Integer[] resid) {
        ActionReturn ar = new ActionReturn();
        try {
            authorityServ.deleteAuthOfRes(resid);
            ar.setStatus(true);
            ar.setMsg("操作成功");
        } catch (Exception e) {
            ar.setStatus(false);
            ar.setMsg("操作失败");
            e.printStackTrace();
        }
        return ar.toString();
    }
}
